package com.fjec.base.controller;

import com.fjec.base.service.ISysAnnexService;
import com.fjec.base.view.AnnexVO;
import com.fjec.common.Const;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.view.AnnexBizItem;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 001.附件表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-08-31
 */
@RestController
@RequestMapping("/")
public class SysAnnexController {

    @Resource
    private ISysAnnexService service;

    /**
     * 上传单个文件
     *
     * @param file    文件
     * @param bizType 业务类型
     * @return
     */
    @PostMapping("/y/annex/store/upload")
    public JsonResult singleUpload(@RequestParam("file") MultipartFile file, Integer bizType) throws Exception {
        if (Const.SECRET_BIZ_TYPE.contains(bizType)) {
            return service.secretFileUpload(file, bizType);
        } else {
            return service.storeFileUpload(file, bizType);
        }
    }

    /**
     * base64文件上传
     *
     * @param file     文件base64
     * @param fileName 文件名
     * @param bizType  业务类型
     * @return
     */
    @PostMapping("/y/annex/base64Upload")
    public JsonResult base64Upload(String file, String fileName, Integer bizType) throws Exception {
        if (Const.SECRET_BIZ_TYPE.contains(bizType)) {
            return service.secretBase64FileUpload(file, fileName, bizType, 1);
        } else {
            return service.storeBase64FileUpload(file, fileName, bizType, 1);
        }
    }

    /**
     * base64文件上传
     *
     * @param file     文件base64
     * @param fileName 文件名
     * @param bizType  业务类型
     * @return
     * @throws Exception
     */
    @PostMapping("/annex/base64Upload")
    public Map<String, Object> rpcBase64Upload(@RequestParam("file") String file,
                                               @RequestParam("fileName") String fileName,
                                               @RequestParam("bizType") Integer bizType,
                                               @RequestParam(value = "bizId", required = false) Long bizId,
                                               @RequestParam(value = "bizCode", required = false) Integer bizCode) throws Exception {
        if (Const.SECRET_BIZ_TYPE.contains(bizType)) {
            return service.rpcSecretBase64FileUpload(file, fileName, bizType, bizId, bizCode, 0);
        } else {
            return service.rpcStoreBase64FileUpload(file, fileName, bizType, bizId, bizCode, 0);
        }
    }

    /**
     * 修改业务数据
     *
     * @param id
     * @param bizId
     * @param bizType
     * @param bizCode
     * @param sort
     * @return
     */
    @PostMapping("/y/annex/uploadBizData")
    public JsonResult uploadBizData(Long id,
                                    @RequestParam(required = false) Long bizId,
                                    @RequestParam(required = false) Integer bizType,
                                    @RequestParam(required = false) Integer bizCode,
                                    @RequestParam(required = false) Integer sort) {
        return service.updateData(id, bizId, bizType, bizCode, sort);
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @PostMapping("/y/annex/logicDelete")
    public JsonResult logicDelete(Long id) {
        return service.logicDelete(id);
    }

    /**
     * 更新附件为可用附件
     *
     * @param itemList
     * @return
     */
    @PostMapping("/annex/available/set")
    public int setAnnexAvailable(@RequestBody List<AnnexBizItem> itemList) {
        return service.setAnnexAvailable(itemList);
    }

    /**
     * 根据业务逻辑获取附件信息
     *
     * @param bizType 业务类型
     * @param bizId   业务Id
     * @param bizCode 业务自定义Code
     * @return
     */
    @GetMapping("/annex/get")
    public List<AnnexVO> getAnnex(Integer bizType,
                                  @RequestParam(required = false) Long bizId,
                                  @RequestParam(required = false) Integer bizCode) throws Exception {
        return service.getAnnex(bizType, bizId, bizCode);
    }

    /**
     * 获取fjsecret下文件
     *
     * @param id
     * @param response
     */
    @GetMapping("/y/annex/secret/get")
    public void getSecretFile(Long id, HttpServletResponse response) throws Exception {
        service.getSecretOSSFile(id, response);
    }

    /**
     * 获取fjsecret下文件，并转换为base64字符串，如果有多个获取第一个
     *
     * @param bizId
     * @param bizType
     * @return
     */
    @GetMapping("/annex/secret/base64/one")
    public String getOneSecretFileBase64(Long bizId, Integer bizType) {
        return service.getOneSecretOSSFileBase64(bizId, bizType);
    }

    /**
     * 根据Id获取文件Base64
     *
     * @param id
     * @return
     */
    @GetMapping("/annex/secret/base64/get")
    public Map<String, Object> getOSSFileBase64ById(Long id) {
//        Map<String, Object> resultMap = new HashMap<>();
//        resultMap.put("base64", service.getOSSFileBase64ById(id));
        return service.getOSSFileBase64ByIdMap(id);
    }

    /**
     * 根据Id获取文件Base64
     *
     * @param id
     * @return
     */
    @GetMapping("/y/annex/secret/base64/get")
    public String getOSSSecretFile(Long id) {
        return service.getOSSFileBase64ById(id);
    }


    /**
     * 获取fjsecret下文件，并转换为base64字符串，如果有多个获取第一个
     *
     * @param bizId
     * @param bizType
     * @return
     */
    @GetMapping("/annex/secret/base64/map")
    public Map<String, Object> getSecretFileBase64Map(@RequestParam Long bizId,
                                                      @RequestParam Integer bizType) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("base64", service.getOneSecretOSSFileBase64(bizId, bizType));
        return resultMap;
    }

}
